home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / crc.swg / 0016_EMSI CRC Procedure.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1995-03-26  |  2.6 KB  |  100 lines

  1. {
  2. > I'm working on a FD replacement... I've got the EMSI stuff... but I
  3. > cant figure out how to calculate the CRCs.....
  4. I know that CRC16('**EMSI_IRQ') should be $8E08, CRC16('**EMSI_REQ')
  5. > should be $A77E. Can anyone provide me with such a CRC function,
  6. > together with an example on how to use it?
  7.  
  8. Could you send me the EMSI specification or Pascal implementation?
  9. Here is CRC code:
  10. }
  11.  
  12. program SerialCRC;      { bit level calculations   jh 3-31-92 }
  13. { HISTORY
  14.    Author:  John Howard
  15.    Original:  03-31-92 1.0
  16.    Material from Jack Crenshaw article in Embedded Systems Programming
  17.    Revision:  10-30-93 2.0
  18.  
  19.    VALIDATE 0.4  method1= CRC-16, method2= CRC-32?
  20. }
  21.  
  22. CONST Feedback =  $8408;   CRCmode = 'CRC-CCITT';
  23.                { $A001;    CRCmode = 'CRC-16'; }
  24.                { $8000;    CRCmode = 'LRCC-16'; }
  25.                { $F0B8;    CRCmode =  'SDLC IBM'; }
  26.  
  27. VAR   CRC : WORD;
  28.  
  29. { Test-case data.
  30.   Data string                       CCITT SDLC  CRC16 XMODEM
  31.  
  32.   'M'                               99E1  9666  35C0  9969
  33.   'T'                               14A1  1B26  FF01  1A71
  34.   'THE'                             7D8D  448E  23B6  1E0A
  35.   'THE,QUICK,BROWN,FOX,0123456789'  7DC5  DF91  B96E  0498
  36. }
  37.  
  38. type HexString = String[9];
  39.  
  40. FUNCTION HexWord(W : Word) : HexString;
  41. const
  42.    HexDigits : ARRAY[0..15] OF Char = '0123456789ABCDEF';
  43. var Temp : HexString;
  44. begin
  45.    Temp[0] := #4;
  46.    Temp[1] := HexDigits[W SHR 12];
  47.    Temp[2] := HexDigits[(W SHR 8) AND $F];
  48.    Temp[3] := HexDigits[(W SHR 4) AND $F];
  49.    Temp[4] := HexDigits[W AND $F];
  50.    HexWord := Temp;
  51. end;
  52.  
  53. { For each byte processed calculate the newest CRC value }
  54. Procedure UpdateCRC(B: Byte);
  55. var i: integer;
  56. begin
  57.    for i := 1 to 8 do begin
  58.       if Odd(B) Xor Odd(CRC) then
  59.          CRC := (CRC SHR 1) XOR Feedback
  60.       else
  61.          CRC := CRC SHR 1;
  62.       B := B SHR 1;
  63.    end;
  64. end;
  65.  
  66. Procedure SendString(S: String);
  67. var   i : integer;
  68. begin
  69.    for i := 1 to length(S) do
  70.       UpdateCRC( ord(S[i]) );
  71. end;
  72.  
  73. Procedure SendMessage(S: String);
  74. var   oldCRC : word;
  75. begin
  76. {   CRC := $FFFF;    SDLC must also complement result }
  77.    CRC := 0;
  78.    SendString(S);
  79.    oldCRC := CRC;
  80.    WriteLN( S);
  81.    Write( CRCmode,' Computed is ');
  82.    WriteLN( HexWord(CRC));
  83.    UpdateCRC(OldCRC);
  84.    UpdateCRC(OldCRC SHR 8);
  85. end;
  86.  
  87. BEGIN  (* **** Main **** *)
  88.    SendMessage('M');
  89.    WriteLN( HexWord(CRC));
  90.  
  91.    SendMessage('T');
  92.    WriteLN( HexWord(CRC));
  93.  
  94.    SendMessage('THE');
  95.    WriteLN( HexWord(CRC));
  96.  
  97.    SendMessage('THE,QUICK,BROWN,FOX,0123456789');
  98.    WriteLN( HexWord(CRC));
  99. END.
  100.